package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.StringUtils;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.goods.dto.FoodsExhibitionHallListDTO;
import com.yuanfeng.goods.dto.GoodsEntityDTO;
import com.yuanfeng.goods.entity.GoodsFoodExhibitionHallEntity;
import com.yuanfeng.goods.mapper.GoodsBaseMapper;
import com.yuanfeng.goods.mapper.GoodsFoodExhibitionHallMapper;
import com.yuanfeng.goods.service.GoodsFoodExhibitionHallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class GoodsFoodExhibitionHallServiceImpl extends ServiceImpl<GoodsFoodExhibitionHallMapper, GoodsFoodExhibitionHallEntity>
    implements GoodsFoodExhibitionHallService{

    @Autowired
    private GoodsBaseMapper goodsBaseMapper;
    @Override
    public ResponseResult queryFoodsHallDetail(FoodsExhibitionHallListDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            GoodsFoodExhibitionHallEntity result = this.baseMapper.selectById(param.getExhibitionHallId());
            String goodsIds = result.getGoodsId();
            List<GoodsEntityDTO> goodsList = new ArrayList<>();
            if (null != goodsIds && !"".equals(goodsIds)) {
                if (goodsIds.contains(",")) {
                    String[] goodsIdss = goodsIds.split(",");
                    for (String s : goodsIdss) {
                        Integer goodsId = Integer.parseInt(s);
                        String goodsName = goodsBaseMapper.selectById(goodsId).getGoodsName();
                        GoodsEntityDTO ge = new GoodsEntityDTO();
                        ge.setGoodsId(goodsId);
                        ge.setGoodsName(goodsName);
                        goodsList.add(ge);
                    }

                } else {
                    Integer goodsId = Integer.parseInt(goodsIds);
                    String goodsName = goodsBaseMapper.selectById(goodsId).getGoodsName();
                    GoodsEntityDTO ge = new GoodsEntityDTO();
                    ge.setGoodsId(goodsId);
                    ge.setGoodsName(goodsName);
                    goodsList.add(ge);
                }
            }
            result.setGoodsList(goodsList);
            return ResponseResult.success(result);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult updateFoodsHallDetail(FoodsExhibitionHallListDTO param) {
        if (param.getExhibitionHallName() == null || "".equals(param.getExhibitionHallName())) {
            return ResponseResult.fail(BizCodeEnume.MISSING_PARAMETER);
        } else if (param.getGoodsId() == null || "".equals(param.getGoodsId())) {
            return ResponseResult.fail(BizCodeEnume.NO_CHOICE_GOODS);
        } else if (param.getExhibitionHallImage() == null || "".equals(param.getExhibitionHallImage())) {
            return ResponseResult.fail(BizCodeEnume.IMAGE_IS_NULL);
        } else if (param.getExhibitionHallLogo() == null || "".equals(param.getExhibitionHallLogo())) {
            return ResponseResult.fail(BizCodeEnume.IMAGE_IS_NULL);
        }
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            this.baseMapper.insertFoodsHallDetail(param);
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult deleteFoodsHallDetail(FoodsExhibitionHallListDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            this.baseMapper.deleteById(param.getExhibitionHallId());
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryFoodsHallList(FoodsExhibitionHallListDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            List<GoodsFoodExhibitionHallEntity> result = this.baseMapper.selectList(new LambdaQueryWrapper<GoodsFoodExhibitionHallEntity>()
                                                         .like(StringUtils.isNotEmpty(param.getExhibitionHallName()),GoodsFoodExhibitionHallEntity::getExhibitionHallName,param.getExhibitionHallName()));

            return ResponseResult.success(result);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




